package pacman;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.*;
import javax.swing.*;
/**
 * The swing Graphic User Interface
 */
public class Gui extends JFrame implements iCtrl, iView {
    
    private cCore parent;
    
    private GamePanel panel;
    private JLabel lblStat;
    
    
    
    private int nextMove=0;

    
    private cPacman pacman;
    
        
    /**
     * Create a new instance of Gui
     * @param parent The cCore game manager of the game
     */
    public Gui(cCore parent) {
        
        this.parent=parent;
        
        panel=new GamePanel(parent);
        lblStat=new JLabel("titi");
        
        pacman=parent.setPacmanControler(this);
        
        
        this.setTitle("TuxMan");
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        this.setResizable(false);

        GroupLayout panelLayout = new GroupLayout(panel);
        panel.setLayout(panelLayout);
        panelLayout.setHorizontalGroup(
            panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, parent.getPlateau().length*40, Short.MAX_VALUE)
        );
        panelLayout.setVerticalGroup(
            panelLayout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGap(0, parent.getPlateau()[0].length*40, Short.MAX_VALUE)
        );
        
        GroupLayout layout = new GroupLayout(this.getContentPane());
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblStat))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addComponent(lblStat)
                .addContainerGap())
        );
     
        this.pack();
       
        this.addKeyListener(new KeyListener() {
            public void keyPressed(KeyEvent keyEvent) {
               //System.out.println(""+keyEvent.getKeyCode());
               if(keyEvent.getKeyCode()==37){
                   nextMove=-1;
               }else if(keyEvent.getKeyCode()==38){
                   nextMove=2;
               }else if(keyEvent.getKeyCode()==39){
                   nextMove=1;
               }else if(keyEvent.getKeyCode()==40){
                   nextMove=-2;
               }else if(keyEvent.getKeyCode()==19){
                   GamePause();
               }
               Redraw();
            }
            public void keyReleased(KeyEvent keyEvent) {
            }
            public void keyTyped(KeyEvent keyEvent) {
            }
        });
        
        this.setVisible(true);  
        
        panel.LoadImages("default");
        
        this.Redraw();
        
    }
    
    /**
     * Turn the game in pause mode
     */
    public void GamePause(){
        parent.Pause();
    }
    

    /**
     * Return the next move of the controled Pacman
     * @return The direction of the next move : -1=Left;1=right;-2=down;2=up
     */
    public int getNextMove() {
        return nextMove;
    }

    /**
     * Redraw the game panel
     */
    public void Redraw() {
        panel.GenImage();
        panel.repaint();
        lblStat.setText("Points : "+pacman.getPoints()+"     Vies : "+pacman.getNbLife());
    }

   
}